package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.TextView;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.WatchFaceStyleAdapter;
import com.umeox.watch.moto.launcher.widget.CurvedScrollBar;

import java.util.Locale;

/**
 * Created by Rambo 2024/6/4/0004
 * Describe:
 */
public class WatchFaceStyleActivity extends ThemeAppCompatActivity implements WatchFaceStyleAdapter.OnClickListener {

    private TextView mStyleTv;
    private ViewPager2 mStyleVP;

    private int currentIconIndex;
    private int[] resIconArr;

    private WatchFaceStyleAdapter mWatchFaceStyleAdapter;

    private CurvedScrollBar scrollBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watchface_style);
        currentIconIndex = getIntent().getIntExtra("extra_watch_index", 0);
        resIconArr = getIntent().getIntArrayExtra("extra_watch_res");
        mStyleTv = findViewById(R.id.tv_style);
        mStyleVP = findViewById(R.id.style_vp);
        scrollBar = findViewById(R.id.scrollbar);
        mStyleTv.setText(String.format(Locale.getDefault(), getString(R.string.str_style), currentIconIndex + 1));
        mWatchFaceStyleAdapter = new WatchFaceStyleAdapter(this,resIconArr);
        mWatchFaceStyleAdapter.setOnClickListener(this);
        mStyleVP.setAdapter(mWatchFaceStyleAdapter);
        mStyleVP.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        mStyleVP.setCurrentItem(currentIconIndex);

        mStyleVP.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mStyleTv.setText(String.format(Locale.getDefault(), getString(R.string.str_style), position + 1));
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                RecyclerView recyclerView = (RecyclerView) mStyleVP.getChildAt(0);
                scrollBar.scroll(recyclerView);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    @Override
    public void onPositionClick(int position) {
        Intent intent = new Intent();
        intent.putExtra("current_style", position);
        setResult(RESULT_OK, intent);
        finish();
    }
}
